/*
 * @copyright 2010 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of My Quick Rules.
 *
 * My Quick Rules is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * My Quick Rules is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with My Quick Rules.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiprion.myquickrules.dao;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.amphiprion.myquickrules.entity.Version;
import org.amphiprion.myquickrules.util.ApplicationConstants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.util.Log;

/**
 * @author Amphiprion
 * 
 */
public class VersionDao extends AbstractDao {
	private static VersionDao instance;

	public static VersionDao getInstance() {
		if (instance == null) {
			instance = new VersionDao();
		}
		return instance;
	}

	public List<Version> getAvailableVersion(String sUrl) {
		ArrayList<Version> versions = new ArrayList<Version>();
		try {
			URL url = new URL(sUrl);
			URLConnection c = url.openConnection();
			InputStream is = c.getInputStream();

			DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
			builderFactory.setNamespaceAware(true);

			DocumentBuilder builder = builderFactory.newDocumentBuilder();
			Document document = builder.parse(is);
			NodeList list = document.getElementsByTagName("version");
			for (int i = 0; i < list.getLength(); i++) {
				Element gameNode = (Element) list.item(i);
				String id = gameNode.getAttributes().getNamedItem("id").getNodeValue();
				String resolution = gameNode.getAttributes().getNamedItem("resolution").getNodeValue();
				String v = gameNode.getAttributes().getNamedItem("version").getNodeValue();
				int length = Integer.parseInt(gameNode.getAttributes().getNamedItem("length").getNodeValue());

				Version version = new Version();
				version.id = id;
				version.resolution = resolution;
				version.version = v;
				version.length = length;
				versions.add(version);
			}
		} catch (Exception e) {
			Log.e(ApplicationConstants.PACKAGE, "getAvailableVersion(String sUrl)", e);
		}
		return versions;
	}

	/**
	 * @param url
	 * @return
	 */
	public Version getVersionInformation(String sUrl) {
		String lng = Locale.getDefault().getLanguage();
		try {
			URL url = new URL(sUrl);
			URLConnection c = url.openConnection();
			InputStream is = c.getInputStream();

			DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
			builderFactory.setNamespaceAware(true);

			DocumentBuilder builder = builderFactory.newDocumentBuilder();
			Document document = builder.parse(is);
			NodeList list = document.getElementsByTagName("version");
			for (int i = 0; i < list.getLength(); i++) {
				Element gameNode = (Element) list.item(i);
				String id = gameNode.getAttributes().getNamedItem("id").getNodeValue();
				String resolution = gameNode.getAttributes().getNamedItem("resolution").getNodeValue();
				String v = gameNode.getAttributes().getNamedItem("version").getNodeValue();
				String creator = getAttribute(gameNode, "creator", "Amphiprion");
				String mail = getAttribute(gameNode, "mail", "amphiprions@gmail.com");
				int length = Integer.parseInt(gameNode.getAttributes().getNamedItem("length").getNodeValue());

				// NAME
				String name = "";
				NodeList langs = gameNode.getElementsByTagName("lang");
				for (int iNames = 0; iNames < langs.getLength(); iNames++) {
					try {
						Element lang = (Element) langs.item(iNames);
						String local = getAttribute(lang, "id", "en");
						if (local.equals(lng)) {
							name = langs.item(iNames).getFirstChild().getNodeValue();
							break;
						} else if ("en".equals(local)) {
							name = langs.item(iNames).getFirstChild().getNodeValue();
						}
					} catch (NullPointerException npe) {
					}
				}

				Version version = new Version();
				version.id = id;
				version.resolution = resolution;
				version.version = v;
				version.length = length;
				version.gameName = name;
				version.creator = creator;
				version.mail = mail;
				return version;
			}
		} catch (Exception e) {
			Log.e(ApplicationConstants.PACKAGE, "getAvailableVersion(String sUrl)", e);
		}
		return null;
	}

}
